//
//  LoginScene.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-6-3.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "LoginScene.h"
#import "UnitProxy.h"
#import "StartScene.h"
#import "Variable.h"
#import "StorageScene.h"


@implementation LoginScene

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
//	LoginScene *layer = [LoginScene node];
    StorageScene* layer = [StorageScene node];
	
	// add layer as a child to scene
	[scene addChild: layer];
    
	// return the scene
	return scene;
}

+(CCScene *) scene2
{
    CCScene *scene = [CCScene node];
    LoginScene *layer = [LoginScene node];
    [scene addChild:layer];
    return scene;
                         
    
}

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) 
    {
        CCMenuItemFont *cancleUserData1;
        CCMenuItemFont *cancleUserData2;
        CCMenuItemFont *cancleUserData3;

        CGSize size = [[CCDirector sharedDirector] winSize];
        ud = [NSUserDefaults standardUserDefaults];  
        
        //给全局变量赋值
        extern NSString* USERDATA_KEY1;
        USERDATA_KEY1 = @"SpaceStorm1";

        extern NSString* USERDATA_KEY2;
        USERDATA_KEY2 = @"SpaceStorm2";
        
        extern NSString* USERDATA_KEY3;
        USERDATA_KEY3 = @"SpaceStorm3";
        
        //零临时的
        CCLabelTTF* label = [CCLabelTTF labelWithString:@"我是可爱的存档选择" fontName:@"AppleGothic" fontSize:16];
        label.color = ccc3(255, 0, 0);
        label.position = CGPointMake(size.width /2, size.height /2 + 150);
        label.anchorPoint = CGPointMake(0.5, 0.5);
        [self addChild:label];
        
        
        //存档1
        udObject = [ud objectForKey:USERDATA_KEY1]; 
        if(udObject != nil)//使用存档数据
        {
            //抽取玩家信息显示
            selectUserData1 = [self getMentItemByIndex:1 isCreate:NO];
        }
        else//数据不存在，创建新存档并使用
        {
            selectUserData1 = [self getMentItemByIndex:1 isCreate:YES];
        }
        //删除存档1
        cancleUserData1 = [CCMenuItemFont itemFromString: @"X" target:self selector:@selector(onCancleUserData1:)];  //清除存档1
        cancleUserData1.position = ccp(50, 150);
        
        //存档2
        udObject = [ud objectForKey:USERDATA_KEY2]; 
        if(udObject != nil)//使用存档数据
        {
            //抽取玩家信息显示
             selectUserData2 = [self getMentItemByIndex:2 isCreate:NO];
        }
        else//数据不存在，创建新存档并使用
        {
            selectUserData2 = [self getMentItemByIndex:2 isCreate:YES];
        }
        //删除存档2
        cancleUserData2 = [CCMenuItemFont itemFromString: @"X" target:self selector:@selector(onCancleUserData2:)];  //清除存档2
        cancleUserData2.position = ccp(50, 130);
        
        //存档3
        udObject = [ud objectForKey:USERDATA_KEY3]; 
        if(udObject != nil)//使用存档数据
        {
            //抽取玩家信息显示
             selectUserData3 = [self getMentItemByIndex:3 isCreate:NO];
        }
        else//数据不存在，创建新存档并使用
        {
            selectUserData3 = [self getMentItemByIndex:3 isCreate:YES];
        }
        //删除存档3
        cancleUserData3 = [CCMenuItemFont itemFromString: @"X" target:self selector:@selector(onCancleUserData3:)];  //清除存档3
        cancleUserData3.position = ccp(50, 110);
        
        menu = [CCMenu menuWithItems:selectUserData1, selectUserData2, selectUserData3, cancleUserData1, cancleUserData2, cancleUserData3,nil];
        menu.position = CGPointZero;
        [self addChild:menu]; 
    }
	return self;
}

/**
	选择并使用第一个存档
	@param sender 
 */
- (void) onSelectUserData1:(id)sender
{
	//载入存档，并从磁盘序列化数据
    [[UnitProxy sharedManager] loadUserDataByKey:USERDATA_KEY1];
    
    //跳转界面
    CCTransitionFade* trans = [CCTransitionFade transitionWithDuration:1.0 scene:[StartScene scene]];
	[[CCDirector sharedDirector] replaceScene: trans];
}

/**
	创建并使用第一个存档
	@param sender 
 */
- (void) onCreateUserData1:(id)sender
{
    //创建一个新存档数据体,并将初始值保存到磁盘
    [[UnitProxy sharedManager] createUserDataByKey:USERDATA_KEY1];
    
    //跳转界面
    CCTransitionFade* trans = [CCTransitionFade transitionWithDuration:1.0 scene:[StartScene scene]];
	[[CCDirector sharedDirector] replaceScene: trans];
}


/**
	清除存档1
	@param sender 
 */
- (void) onCancleUserData1:(id)sender
{
    //清除存档数据
    [ud removeObjectForKey:USERDATA_KEY1];
    
    //移除老的菜单项
    [menu removeChild:selectUserData1 cleanup:YES];
    
    //创建新的菜单项
    CCMenuItemFont * selectUserData = [self getMentItemByIndex:1 isCreate:YES]; 
    [menu addChild:selectUserData];
}

/**
	选择并使用第二个存档
	@param sender 
 */
- (void) onSelectUserData2:(id)sender
{
    //载入存档，并从磁盘序列化数据
    [[UnitProxy sharedManager] loadUserDataByKey:USERDATA_KEY2];
    
    //跳转界面
    CCTransitionFade* trans = [CCTransitionFade transitionWithDuration:1.0 scene:[StartScene scene]];
	[[CCDirector sharedDirector] replaceScene: trans];
}

/**
	创建并使用第二个存档
	@param sender 
 */
- (void) onCreateUserData2:(id)sender
{
	//创建一个新存档数据体,并将初始值保存到磁盘
    [[UnitProxy sharedManager] createUserDataByKey:USERDATA_KEY2];
    
    //跳转界面
    CCTransitionFade* trans = [CCTransitionFade transitionWithDuration:1.0 scene:[StartScene scene]];
	[[CCDirector sharedDirector] replaceScene: trans];
}

/**
 清除存档2
 @param sender 
 */
- (void) onCancleUserData2:(id)sender
{
    //清除存档数据
    [ud removeObjectForKey:USERDATA_KEY2];
    
    //移除老的菜单项
    [menu removeChild:selectUserData2 cleanup:YES];
    
    //创建新的菜单项
    CCMenuItemFont * selectUserData = [self getMentItemByIndex:2 isCreate:YES]; 
    [menu addChild:selectUserData];
}

/**
	选择使用第三个存档
	@param sender 
 */
- (void) onSelectUserData3:(id)sender
{
	//载入存档，并从磁盘序列化数据
    [[UnitProxy sharedManager] loadUserDataByKey:USERDATA_KEY3];
    
    //跳转界面
    CCTransitionFade* trans = [CCTransitionFade transitionWithDuration:1.0 scene:[StartScene scene]];
	[[CCDirector sharedDirector] replaceScene: trans];
}

/**
	创建并使用第三个存档
	@param sender 
 */
- (void) onCreateUserData3:(id)sender
{
    //创建一个新存档数据体,并将初始值保存到磁盘
    [[UnitProxy sharedManager] createUserDataByKey:USERDATA_KEY3];
    
    //跳转界面
    CCTransitionFade* trans = [CCTransitionFade transitionWithDuration:1.0 scene:[StartScene scene]];
	[[CCDirector sharedDirector] replaceScene: trans];
}

/**
 清除存档3
 @param sender 
 */
- (void) onCancleUserData3:(id)sender
{
    //清除存档数据
    [ud removeObjectForKey:USERDATA_KEY3];
    
    //移除老的菜单项
    [menu removeChild:selectUserData3 cleanup:YES];
    
    //创建新的菜单项
    CCMenuItemFont * selectUserData = [self getMentItemByIndex:3 isCreate:YES]; 
    [menu addChild:selectUserData];
}

/**
	根据用户数据，返回菜单项的名称，
    默认格式：角色头像 军衔 游戏章节（2-3）
	@param userData 用户数据
	@returns 菜单项名称
 */
-(NSString*) getMenuItemNameByUserData:(UserData*)userData
{
    NSString* str = [NSString stringWithFormat:@"level:%d_exp:%d_money:%D",
                     userData.level, userData.exp, userData.money];
    return str;
}

/**
	返回创建的菜单项
	@param index 所在的存档位置
	@param isCreate 是不是创建新存档（不然就是读取存档）
	@returns 菜单项
 */
- (CCMenuItemFont*) getMentItemByIndex:(int)index isCreate:(BOOL)isCreate
{
    CCMenuItemFont* menuItem;
    
    //创建3个存档按钮和删除按钮
    id color_action = [CCTintBy actionWithDuration:0.5f red:0 green:-255 blue:-255];  
    id color_back = [color_action reverse];  
    id seq = [CCSequence actions:color_action, color_back, nil];  
    
    switch (index) 
    {
        case 1://存档1
        {
            if(isCreate == YES)//创建新存档
            {
                menuItem = [CCMenuItemFont itemFromString: @"CreateNew1" target:self selector:@selector(onCreateUserData1:)];
                [menuItem runAction:[CCRepeatForever actionWithAction:seq]];  
                menuItem.position = ccp(200, 150);
            }
            else//使用现有存档
            {
                UserData* userData = [NSKeyedUnarchiver unarchiveObjectWithData:udObject]; 
                menuItem = [CCMenuItemFont itemFromString:[self getMenuItemNameByUserData:userData] target:self selector:@selector(onSelectUserData1:)];
                [menuItem runAction:[CCRepeatForever actionWithAction:seq]];  
                menuItem.position = ccp(200, 150);
            }
            break; 
        }
        case 2://存档2
        {
            if(isCreate == YES)//创建新存档
            {
                menuItem = [CCMenuItemFont itemFromString: @"CreateNew2" target:self selector:@selector(onCreateUserData2:)];  //选择存档2
                [menuItem runAction:[CCRepeatForever actionWithAction:seq]];  
                menuItem.position = ccp(200, 130);
            }
            else//使用现有存档
            {
                UserData* userData = [NSKeyedUnarchiver unarchiveObjectWithData:udObject]; 
                menuItem = [CCMenuItemFont itemFromString:[self getMenuItemNameByUserData:userData] target:self selector:@selector(onSelectUserData2:)];  //选择存档2
                [menuItem runAction:[CCRepeatForever actionWithAction:seq]];  
                menuItem.position = ccp(200, 130);
            }
            break; 
        }
        case 3://存档3
        {
            if(isCreate == YES)//创建新存档
            {
                menuItem = [CCMenuItemFont itemFromString: @"CreateNew3" target:self selector:@selector(onCreateUserData3:)];  //选择存档3
                [menuItem runAction:[CCRepeatForever actionWithAction:seq]];  
                menuItem.position = ccp(200, 110);
            }
            else//使用现有存档
            {
                UserData* userData = [NSKeyedUnarchiver unarchiveObjectWithData:udObject]; 
                menuItem = [CCMenuItemFont itemFromString:[self getMenuItemNameByUserData:userData] target:self selector:@selector(onSelectUserData3:)];  //选择存档3
                [menuItem runAction:[CCRepeatForever actionWithAction:seq]];  
                menuItem.position = ccp(200, 110);
            }
            break; 
        }
        default:
            break;
    }
    
    return menuItem;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
